<?php

class Backend_BookingController extends Zend_Controller_Action
{

    protected $_redirector = null;

    public function init()
    {
        /* Initialize action controller here */
        $this->_helper->layout->setLayout('backend/layout');
        $this->view->title = "";
        $this->view->bookingctrl = true;

        $this->_redirector = $this->_helper->getHelper("Redirector");

        // check session
        @session_start();
        if(!isset($_SESSION['admin'])){
            $this->_redirector->gotoUrl('/backend/auth/login');
        }
        else{
            //role must be sales
            if($_SESSION['admin']['role_id']==2){
                $this->_redirector->gotoUrl('/backend/');
            }

            $this->view->session = $_SESSION['admin'];
        }
    }

    public function indexAction()
    {
        $this->_redirector->gotoUrl('/backend/booking/unapproved');
    }

    public function unapprovedAction()
    {
        $this->view->title = "Unapproved Bookings";

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            $booking_db = new Application_Model_DbTable_Booking();

            if(strcmp($postData['act'],'approve')==0){
                $booking_db->update(array('booking_status_id'=>2), 'booking_id = '.$postData['id']);
            }
            else if(strcmp($postData['act'],'cancel')==0){
                $booking_db->cancelbooking($postData['id'], $postData['reason']);
            }

            $this->_redirector->gotoUrl('/backend/booking/unapproved');
        }

        $admin_id = $_SESSION['admin']['admin_id'];
        $booking_db = new Backend_Model_DbTable_Booking();
        $this->view->bookings = $booking_db->getAllBookings(array('b.booking_status_id = 1',"sales_id = $admin_id"));
    }

    public function unpaidAction()
    {
        $this->view->title = "Unpaid Bookings";

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            $booking_db = new Application_Model_DbTable_Booking();

            if(strcmp($postData['act'],'cancel')==0){
                $booking_db->cancelbooking($postData['id'], $postData['reason']);
            }

            $this->_redirector->gotoUrl('/backend/booking/unpaid');
        }

        $admin_id = $_SESSION['admin']['admin_id'];
        $booking_db = new Backend_Model_DbTable_Booking();
        $this->view->bookings = $booking_db->getAllBookings(array('b.booking_status_id > 1','b.payment_status_id < 3',"sales_id = $admin_id"));
    }

    public function completedAction()
    {
        $this->view->title = "Completed Bookings";

        $admin_id = $_SESSION['admin']['admin_id'];
        $booking_db = new Backend_Model_DbTable_Booking();
        $this->view->bookings = $booking_db->getAllBookings(array('b.booking_status_id = 3',"sales_id = $admin_id"));
    }

    public function canceledAction()
    {
        $this->view->title = "Canceled Bookings";

        $admin_id = $_SESSION['admin']['admin_id'];
        $booking_db = new Backend_Model_DbTable_Booking();
        $this->view->bookings = $booking_db->getAllBookings(array('b.booking_status_id < 0',"sales_id = $admin_id"),true);
    }

    public function detailsAction()
    {
        $this->view->title = "Booking Details";
        $bid = $this->getRequest()->getParam('bid',0);
        $this->view->bid = $bid;

        if($this->getRequest()->isPost()){
            $this->view->prev = $this->getRequest()->getParam('prev');
        }

        if($bid > 0){
            $booking_db = new Backend_Model_DbTable_Booking();
            $this->view->details = $booking_db->getBooking('booking_id', (int)$bid);
        }
        else{
            $this->_redirector->gotoUrl('/backend/booking');
        }
    }


}











